#include<iostream>
#include"newtoninterpolation.h"
#include"polymerization.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{

    datatalbe maintable;
    cout<<"intput data like x f(x),q to quit"<<endl;
    int x,y;
    while(cin>>x>>y)
    {

        maintable.addnumber(x,y);
        cout<<"intput data like x f(x),q to quit"<<endl;
    }
    cin.clear();
    cin.get();
    maintable.produce();
    maintable.show();
    polymerization timepoly;// save (x-x0)(x-x1) while the program is runing
    polymerization resultpoly;
    polymerization * temppoly =new polymerization;
    polymerization timetemppoly;
    polymerization coefficientpoly;
    int count=maintable.getcount();
    temppoly->setpoly(2,(double)1,(int)0);
    timepoly.setpoly(2,(double)1,(int)0);
    for(int i=1;i<=count;i++)
    {
        if(i==1)
        {
        resultpoly.setpoly(2,(double)maintable.getdata(i,0),(int)0);
        continue;
        }
        timetemppoly.setpoly(4,(double)1,(int)1,(double)-(maintable.getx(i-1)),(int)0);
        timepoly=timepoly*timetemppoly;
        coefficientpoly.setpoly(2,(double)maintable.getdata(i,i-1),(int)0);
        *temppoly=coefficientpoly*timepoly;
        resultpoly=resultpoly+*temppoly;
        delete temppoly;
        temppoly =new  polymerization;

    }
    resultpoly.show();
    cout<<"input the x you want to get"<<endl;
    double x2;
    cin>>x2;
    cout<<resultpoly.substitute(x2);
}
